{% extends '../../common/layouts/layout.tpl' %}
{% block content %}
	 <div class="container p-login">
            <div class="column">
                <div class="login-form">
                    <div class="login-title">登录ThankFund</div>
                    <div class="login-inner mt-60">
                        <form class="layui-form">
                            <div class="login-account">
                                <input type="text" name="mobile" lay-verify="mobile" value="" placeholder="手机号">
                            </div>
                            <div class="login-pwd mt-30">
                                <input type="password" name="password" lay-verify="password" value="" placeholder="密码">
                            </div>

                            <div class="layui-form-item mt-20" id="captcha-item" style="display: none;">
                                <input type="text" name="captcha" lay-verify="captcha" placeholder="验证码" class="layui-input captch-input" >
                                <img src="/common/captcha/get" onclick="this.src='/common/captcha/get?r='+Math.random();"/>
                            </div>
                            <div class="f-r mt-10">
                                <a href="register" class="reg-link">免费注册</a>|<a href="#" class="get-pwd">忘记密码</a>
                            </div>
                            <div class="login-but mt-80">
                                <button lay-submit="" lay-filter="user-login-submit" onclick="return false;">登 录</button>
                            </div>
                            <span class="reg-declaration mt-30">
                                <input type="checkbox" lay-skin="primary" checked="checked" id="i-do" title="我已经阅读并同意<a href='#'>《ThankFund注册协议》</a>">

                            </span>
                        </form>
                    </div>
                </div>
            </div>
        </div>
{% endblock %}

{% block js_assets %}
    <script>
        $(function(){
            var isShow = getCookie("captcha_show");
            if (isShow == 1) {
                $("#captcha-item img").click();
                $("#captcha-item").show();
            }else {
                $("#captcha-item").hide();
            }
        });

        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer

            form.verify({
                mobile: function (value) {
                    var reg=/^1[3|4|5|7|8][0-9]{9}$/;
                    if(!reg.test(value)){
                        return '手机号码格式有误';
                    }
                },
                captcha: function (value) {
                    if (!$("#captcha-item").is(":hidden")) {
                        if (value.trim().length == 0) {
                            return '请输入验证码'
                        }
                    }
                },
                password: function (value) {
                    if (value.length == 0) {
                        return '请输入密码';
                    }
                }
                /* ,password: [passwordReg, '请输入正确的密码']*/
            });

            //监听提交
            form.on('submit(user-login-submit)', function (data) {
                if (!$("#captcha-item").is(":hidden")) {
                    checkCaptcha(data.field);
                } else {
                    signup(data.field);
                }
                return false;
            });
        });

        function signup(formData) {
            $.post("/signup_mobile", formData, function (result) {
                var path = "/";
                if (result.status != 200) {
                    setCaptcha(path);
                    layer.msg(result.msg || "密码或账号错误", {icon: 2, time: 500});
                    return false
                }

                var datas = JSON.parse(result.datas);
                var user = {
                    role_type: datas.role_type,
                    user_id: datas.user_id,
                    email: datas.user.email
                };
                console.log(datas)
                addCookie("captcha_show", false, 0, path);
                addCookie("token", datas.token, 0, path);
                addCookie("user", JSON.stringify(user), 0, path);
                layer.msg("登录成功", {icon: 1, time: 1000,}, function () {
                    //登录成功跳转页
                    window.location.href = ""
                }, 100)
            });
        }
    </script>
{% endblock %}